<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="物料编号" prop="bpsSn" label-width="55">
        <el-input
          v-model="queryParams.orderSn"
          placeholder="请输入物料编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['mes:retrieval:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['mes:retrieval:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['mes:retrieval:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="retrievalList"border style="width: 100%" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
<!--      <el-table-column label="id" align="center" prop="id" />-->
      <el-table-column label="状态" align="center" prop="dfsState" >
        <template v-slot = "scope">
          <dict-tag :options="dict.type.storage_out_state" :value="scope.row.dfsState"/>
        </template>
      </el-table-column>
      <el-table-column label="出库编号" align="center" prop="dfsSn" />
<!--      <el-table-column label="产品库存id" align="center" prop="proStrmId" />-->
<!--      <el-table-column label="出库数量" align="center" prop="dfsNum" />-->
      <el-table-column label="出库人" align="center" prop="userId" />
      <el-table-column label="车间任务编号" align="center" prop="sptkSn" />
<!--      <el-table-column label="出库类型" align="center" prop="dfsType" />-->
      <el-table-column label="出库日期" align="center" prop="dfsDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dfsDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建者" align="center" prop="createdBy" />
      <el-table-column label="创建时间" align="center" prop="createdTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row,scope.$index)"
            v-hasPermi="['mes:retrieval:edit']"
          >出库</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改出库管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1300px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-divider>物料列表</el-divider><!--分割线-->
        <el-card shadow="always" style="border-radius: 15px;" >
          <el-table border style="width: 100%" v-loading="loading" :data="mobDetailesList">
            <el-table-column label="物料名称" prop="matName">
            </el-table-column>
            <el-table-column label="物料编码" prop="matSn">
            </el-table-column>
            <el-table-column label="原料库" prop="strmName" :width="80">
            </el-table-column>
            <el-table-column label="供应商" prop="supplierName" :width="80">
            </el-table-column>
<!--            <el-table-column label="仓库容量" :width="280">-->
<!--              <template slot-scope="scope">-->
<!--                <el-row type="flex" align="middle">-->
<!--                  <el-col :span="10">-->
<!--                    <span>总容量: {{ scope.row.strmNum }}</span>-->
<!--                  </el-col>-->
<!--                  <el-col :span="14">-->
<!--                    <span>可用容量: {{ scope.row.strmNum-scope.row.strmRenum }}</span>-->
<!--                  </el-col>-->
<!--                </el-row>-->
<!--              </template>-->
<!--            </el-table-column>-->
            <el-table-column label="需出库数量" prop="num" :width="100">
              <template slot-scope="scope">
                <span>{{scope.row.num+0}}</span>
              </template>
            </el-table-column>
            <el-table-column label="已出库数量" prop="renum" :width="100">
              <template slot-scope="scope">
                <span>{{scope.row.renum+0}}</span>
              </template>
            </el-table-column>
            <el-table-column label="仓库剩余数量" prop="leftNum" :width="100">
              <template slot-scope="scope">
                <span>{{scope.row.leftNum+0}}</span>
              </template>
            </el-table-column>
            <el-table-column label="出库数量">
              <template slot-scope="scope">
                <el-input-number v-if="scope.row.leftNum>(scope.row.num-scope.row.renum)" v-model="scope.row.tempNum" controls-position="right"  :min="0" :max="scope.row.num-scope.row.renum"></el-input-number>
                <el-input-number v-else v-model="scope.row.tempNum" controls-position="right"  :min="0" :max="scope.row.leftNum"></el-input-number>

              </template>
            </el-table-column>
<!--            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--              <template slot-scope="scope">-->
<!--                <el-button  size="mini" type="text" icon="el-icon-check" @click="mtroutadd(scope.$index)">增加</el-button>-->
<!--                <el-button  size="mini" type="text" icon="el-icon-check" @click="mtroutdel(scope.$index)">删除</el-button>-->
<!--              </template>-->
<!--            </el-table-column>-->
          </el-table>
        </el-card>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRetrieval, getRetrieval, delRetrieval, addRetrieval, updateRetrieval, getRetrievalMob, updateRetrievalMob} from "@/api/mes/retrieval";

export default {
  name: "Retrieval",
  dicts:["storage_out_state"],
  data() {
    return {
      Index:0,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 出库管理表格数据
      retrievalList: [],
      //物料出库详情
      mobDetailesList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询出库管理列表 */
    getList() {
      this.loading = true;
      const dfsType = 1;
      listRetrieval(this.queryParams).then(response => {
        this.retrievalList = response.rows;
        this.retrievalList = this.retrievalList.filter(item => item.dfsType == dfsType)
        console.log("this.retrievalList",this.retrievalList)
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        dfsSn: null,
        proStrmId: null,
        dfsNum: null,
        userId: null,
        orderId: null,
        dfsType: null,
        dfsDate: null,
        dfsState: null,
        isDeleted: null,
        createdBy: null,
        createdTime: null,
        updatedBy: null,
        updatedDate: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加出库管理";
    },
    /** 出库按钮操作 */
    handleUpdate(row,index) {
      this.reset();
      this.Index=index;
      const id = row.id || this.ids
      getRetrieval(id).then(response =>{
        this.form=response.data;
        getRetrievalMob(row.mldId).then(response => {
          console.log("fafwadadw",response)
          this.mobDetailesList = response.rows;
          this.open = true;
          this.title = "物料出库确认";
        });
      });
    },
    /** 提交按钮 */
    submitForm() {
      var Num=0;
      var Renum=0;
      var AllNum=0;
      this.mobDetailesList.forEach((item)=>{
        item.matStrmId=item.matId;
        item.id = this.retrievalList[this.Index].id;
        Num=Num+item.tempNum;
        Renum=Renum+item.renum;
        AllNum=AllNum+item.num;
      });
      if((Num+Renum)<AllNum){
        this.form.dfsState=1;
      }
      else {
        this.form.dfsState=2;
      }
      console.log("this.mobDetailesList",this.form)
      updateRetrieval(this.form).then(response => {
        updateRetrievalMob(this.mobDetailesList).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.open = false;
          this.getList();
        });
      });

      // this.$refs["form"].validate(valid => {
      //   if (valid) {
      //     if (this.form.id != null) {
      //       updateRetrieval(this.form).then(response => {
      //         this.$modal.msgSuccess("修改成功");
      //         this.open = false;
      //         this.getList();
      //       });
      //     } else {
      //       addRetrieval(this.form).then(response => {
      //         this.$modal.msgSuccess("新增成功");
      //         this.open = false;
      //         this.getList();
      //       });
      //     }
      //   }
      // });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除出库管理编号为"' + ids + '"的数据项？').then(function() {
        return delRetrieval(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mes/retrieval/export', {
        ...this.queryParams
      }, `retrieval_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
